// packageC/pages/package/gonzhuangronqiguanli/5S_manage/5S_handling/handling.js
const mybehavior = require("../../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    fs_upload_url: "https://logisticsengineering.gree.com/scene_api/photo_manage",
    baseUrl: "https://logisticsengineering.gree.com/scene_api/fiveS_manage",
    page: 1,
    page_size: 10,
    activeNamesDetail: ['1'],
    activeNamesStep: ['1'],
    problem_source: "监督检查",
    steps: [

    ],
    active: 0,
    now_steps: {},
    opration: {
      "问题提交": ["责任单位整改报完工", "申请延期"],
      "责任单位整改报完工": ["确认整改完成", "确认未完成整改"],
      "确认整改完成": [],
      "确认未完成整改": ["责任单位第2次整改报完工"],
      "责任单位第2次整改报完工": ["确认第二次整改完成", "确认第二次整改未完成"],
      "确认第二次整改完成": [],
      "确认第二次整改未完成": [],
      "申请延期": ["同意延期", "不同意延期"],
      "同意延期": ["责任单位整改报完工"],
      "不同意延期": ["责任单位整改报完工"],
    },
    map: {
      "问题提交": ["报完工", "申请延期"],
      "责任单位整改报完工": ["整改完成", "未整改完成"],
      "确认整改完成": [],
      "确认未完成整改": ["报完工"],
      "责任单位第2次整改报完工": ["整改完成", "未整改完成"],
      "确认第二次整改完成": [],
      "确认第二次整改未完成": [],
      "申请延期": ["同意", "不同意"],
      "同意延期": ["报完工"],
      "不同意延期": ["报完工"],
    },
    btn_list: [],
    zgwg_show: false,
    yqzg_show: false,
    wg_show: false,
    action: "",
    files: [],
    reason: "",
    deadline: "",
    back_flag: false,

  },
  // 上传照片
  getUploaderVlaue(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
  },
  // 时分的事件方法 
  selectDateMinuteChange(e) {
    let now_time = this.formatNowTime()
    console.log(now_time);
    console.log(e.detail.value);
    console.log(e.detail.value < now_time);
    if (e.detail.value < now_time) {
      return wx.showToast({
        title: '需求时间要求大于当前时间',
        icon: 'none'
      })
    } else {
      this.setData({
        deadline: e.detail.value
      })
    }

  },

  // 展开详情
  onActiveDetailChange(event) {
    this.setData({
      activeNamesDetail: event.detail,
    });
  },
  // 展开步骤
  onActiveChangeStep(event) {
    this.setData({
      activeNamesStep: event.detail,
    });
  },
  // 处理签审日志数据
  handleStep() {
    let live_record1 = JSON.parse(JSON.stringify(this.data.task_data.live_record))
    for (let i in live_record1) {
      live_record1[i].time = this.GMTToStr(live_record1[i].time)
    }
    let step = []
    for (let i in live_record1) {
      let obj = {
        // text: live_record1[i].action,
        desc: live_record1[i].involved_name + "，" + live_record1[i].time + "，" + live_record1[i].action,
        detail: live_record1[i]
      }
      step.push(obj)
    }
    this.setData({
      steps: step,
      active: live_record1.length - 1
    })


  },
  // 点击记录步骤详情弹窗显示
  showStep(e) {
    let step_arr = JSON.parse(JSON.stringify(this.data.steps[e.detail].detail))
    let task_data = JSON.parse(JSON.stringify(this.data.task_data))
    let arr = []
    if (step_arr.files.length != 0) {
      for (let i = 0; i < step_arr.files.length; i++) {
        let obj = {
          url: this.data.fs_upload_url + '/fs_download?file_id=' + step_arr.files[i],
          isImage: true,
          file_id: step_arr.files[i]
        }
        arr.push(obj)

      }
    }
    if (step_arr.action == "申请延期") {
      this.setData({
        date: task_data.pre_rect_deadline,
        date_show1: true
      })

    }
    if (step_arr.action == "同意延期") {
      this.setData({
        date: task_data.rect_deadline,
        date_show1: true
      })

    }
    step_arr.files = arr
    this.setData({
      now_steps: step_arr,
      step_detail_show: true
    })

  },
  // 点击记录步骤详情弹窗关闭
  onStepClose() {
    this.setData({
      now_steps: {},
      step_detail_show: false,
      date_show1: false,
      date: "",

    })
  },
  // 处理步骤状态按钮显示
  handleStats() {
    let status = this.data.task_data.status
    let map_arr = this.data.map
    let map_keys = Object.keys(this.data.map)
    for (let i = 0; i < map_keys.length; i++) {
      if (map_keys[i] == status) {
        this.setData({
          btn_list: map_arr[map_keys[i]]
        })
      }

    }
  },
  // 处理按钮
  handleBtn(e) {
    console.log(e);
    let index = e.currentTarget.dataset.key
    let key_str = e.currentTarget.dataset.key_str
    let opration_arr = this.data.opration
    let current_status = this.data.task_data.status
    console.log("传过去的状态", opration_arr[current_status][index]);
    this.setData({
      action: opration_arr[current_status][index]
    })
    // 整改弹窗
    if (key_str == "报完工") {
      this.setData({
        zgwg_show: true
      })
    }
    // 判断是否完成整改弹窗
    if (key_str == "整改完成" || key_str == "未整改完成" || key_str == "不同意") {
      this.setData({
        wg_show: true
      })
    }
    // 申请延期整改弹窗
    if (key_str == "同意" || key_str == "申请延期") {
      this.setData({
        yqzg_show: true
      })
    }

  },
  // 整改弹窗确认
  onConfirm1() {
    if (this.data.reason == "" || this.data.files.length == 0) {
      wx.showToast({
        title: '说明、整改照片不能为空',
        icon: 'none'
      })
    } else {
      let url = this.data.baseUrl + "/update_5s_problem"
      let img_arr = []
      if (this.data.files.length != 0) {
        for (let i = 0; i < this.data.files.length; i++) {
          img_arr.push(this.data.files[i].file_id);
        }
      }
      let data = {
        key: this.data.task_data.key,
        status: this.data.task_data.status,
        res_manager: this.data.task_data.res_manager,
        submitter: this.data.task_data.submitter,
        action: this.data.action,
        reason: this.data.reason,
        args: [],
        files: img_arr,
        deadline: this.data.deadline,

      }
      console.log(data);
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/packageC/pages/package/gonzhuangronqiguanli/5S_manage/5S_handling_list/5S_handling_list',
            })
          }, 2000)
        } else {

          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {

        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
  },
  // 整改弹窗关闭
  onHandleClose1() {
    this.setData({
      zgwg_show: false,
      reason: "",
      action: "",
      files: [],

    })
  },
  //申请延期整改弹窗确认
  onConfirm2() {
    console.log(this.data.deadline);
    if (this.data.reason == "" || this.data.deadline == "") {
      wx.showToast({
        title: '说明、延期时间不能为空',
        icon: 'none'
      })
    } else {
      let url = this.data.baseUrl + "/update_5s_problem"
      let img_arr = []
      if (this.data.files.length != 0) {
        for (let i = 0; i < this.data.files.length; i++) {
          img_arr.push(this.data.files[i].file_id);
        }
      }
      let data = {
        key: this.data.task_data.key,
        status: this.data.task_data.status,
        res_manager: this.data.task_data.res_manager,
        submitter: this.data.task_data.submitter,
        action: this.data.action,
        reason: this.data.reason,
        args: [],
        files: img_arr,
        deadline: this.data.deadline,

      }
      console.log(data);
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/packageC/pages/package/gonzhuangronqiguanli/5S_manage/5S_handling_list/5S_handling_list',
            })
          }, 2000)
        } else {

          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {

        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
  },
  // 申请延期整改弹窗关闭
  onHandleClose2() {
    this.setData({
      yqzg_show: false,
      action: "",
      reason: "",
    })
  },
  //申请延期整改弹窗确认
  onConfirm3() {
    if (this.data.reason == "") {
      wx.showToast({
        title: '说明不能为空',
        icon: 'none'
      })
    } else {
      let url = this.data.baseUrl + "/update_5s_problem"
      let img_arr = []
      if (this.data.files.length != 0) {
        for (let i = 0; i < this.data.files.length; i++) {
          img_arr.push(this.data.files[i].file_id);
        }
      }
      let data = {
        key: this.data.task_data.key,
        status: this.data.task_data.status,
        res_manager: this.data.task_data.res_manager,
        submitter: this.data.task_data.submitter,
        action: this.data.action,
        reason: this.data.reason,
        args: [],
        files: img_arr,
        deadline: this.data.deadline,

      }
      console.log(data);
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/packageC/pages/package/gonzhuangronqiguanli/5S_manage/5S_handling_list/5S_handling_list',
            })
          }, 2000)
        } else {

          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {

        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
  },
  // 判断是否完成整改弹窗关闭
  onHandleClose3() {
    this.setData({
      wg_show: false,
      reason: "",
    })
  },
 // 撤销数据   
 callBackBtn() {
  wx.showModal({
    title: '确认撤销此单据？',
    content: '',
    complete: (res) => {
      if (res.cancel) {
        
      }
  
      if (res.confirm) {
        let url = this.data.baseUrl + "/recall_process_problem"
        
        let data = {
          key: this.data.task_data.key,
          status: this.data.task_data.status,
          submitter: this.data.task_data.submitter,
          reason: this.data.reason,
  
        }
        console.log(data);
        this.post(url, data).then(res => {
          if (res.code == 20000) {
            wx.showToast({
              title: res.msg,
            })
            setTimeout(function () {
              wx.reLaunch({
                  url: '/packageB/pages/package/task_manager/task_manager',
              })
            }, 2000)
          } else {
  
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
          }
        }).catch(err => {
  
          console.log(err);
          wx.showToast({
            title: err.msg,
            icon: 'error'
          })
        })
      }
    }
  })



},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let self = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log("传过来的数据", data)
      let arr = []
      data.submit_rect_deadline = self.GMTToStr(data.submit_rect_deadline)
      for (let i in data.files) {
        let obj = {
          url: self.data.fs_upload_url + '/fs_download?file_id=' + data.files[i],
          isImage: true,
          file_id: data.files[i]
        }
        arr.push(obj)
      }
      data.files = arr
      data.business_content = '业务内容：处理期限：' + data.rect_deadline + '；流程：' + data.status + '，责任单位：' + data.res_accurate
      if (data.pre_rect_deadline != null) {
        data.pre_rect_deadline = self.GMTToStr(data.pre_rect_deadline)
      }
      if (data.live_record.length == 1) {
        self.setData({
          back_flag: true,

        })
      }
      self.setData({
        task_data: data,
        deadline: data.pre_rect_deadline,
      })
      // 处理签审日志
      self.handleStep()
      // 处理状态按钮
      self.handleStats()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})